$(function(){
    function popFn(){
        popHtml = ''
        popHtml += '<div id="graybg_pop" class="graybg_pop ie6fixed_t" style="display:block"></div>'
        popHtml += '<div class="col-lg-3 teacher_pop friend_pop student_pop" style="height:320px;margin-top:-160px;display:block">'
        popHtml += '    <span class="teacher_tit">添加</span>'
        popHtml += '    <i class="fa fa-times" aria-hidden="true"></i>'

        popHtml += '    <div class="form-group">'
        popHtml += '        <label>账户</label>'
        popHtml += '        <input class="form-control tel"  placeholder="请输入帐号，必须是手机号">'
        popHtml += '    </div>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>密码</label>'
        popHtml += '        <input class="form-control pass" type="password"  placeholder="请输入密码">'
        popHtml += '    </div>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>邮箱</label>'
        popHtml += '        <input class="form-control email" type="text"  placeholder="请输入邮箱">'
        popHtml += '    </div>'
        popHtml += '    <p class="p_btn"><button type="button" class="btn btn-lg btn-success add_ok" >确定</button></p>'
        popHtml += '</div>'
    }

    admin.ajaxFn(
        {
            url:admin.baseUrl.operateUserList,
            type : 'GET',
            data : {
                page_size : 20,
                page : 1
            }
        },
        function(data){
            $('#operate').html(template('operateTpl',{'res': data}));
        }
    )

    //修改状态
    $('body').on( 'click' ,'.status_btn' , function(){
        var dataid = $(this).attr('data_id')
        var datatype = $(this).attr('data_type')
        admin.ajaxFn(
            {
                url:admin.baseUrl.operateUserListDisable,
                type : 'POST',
                data : {
                    id : dataid,
                    status : datatype
                }
            },
            function(data){
                //友情列表
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.operateUserList,
                        type : 'GET',
                        data : {
                            page_size : 20,
                            page : 1
                        }
                    },
                    function(data){
                        $('#operate').html(template('operateTpl',{'res': data}));
                    }
                )
            }
        )
    })




    //添加
    $('body').on( 'click' ,'.add_list' , function(){
        popFn(1 , '');
        $('body').append(popHtml)

    })

    
    //确定
    $('body').on( 'click' ,'.add_ok' , function(){
        var telVal = $('.tel').val()
        var passVal = $('.pass').val()
        var ssfVal = $('.email').val()
        var telTest = /^1(3|4|5|6|7|8|9)\d{9}$/
        var email = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        
            if(telVal.length <= 0){
                alert('请输入手机,必须是手机号')
                return false
            }else if(!(telTest.test(telVal))){
                alert('请填写正确手机号！')
                $('.tel').val("")
                return false
            }else if(passVal.length <= 0 ){
                alert('请输入密码')
                return false
            }else if(passVal.length < 10){
                alert('密码最少10位数')
                return false
            }else if(ssfVal.length <= 0){
                alert('请输入邮箱')
                return false
            }else if(!(email.test(ssfVal))){
                alert('请填写正确邮箱')
                return false
            }else{
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.operateUserListAdd,
                        type : 'POST',
                        data : {
                            username : telVal,
                            password : passVal,
                            email : ssfVal
                        }
                    },
                    function(data){
                        admin.ajaxFn(
                            {
                                url:admin.baseUrl.operateUserList,
                                type : 'GET',
                                data : {
                                    page_size : 20,
                                    page : 1
                                }
                            },
                            function(data){
                                $('#graybg_pop , .teacher_pop').remove();
                                $('#operate').html(template('operateTpl',{'res': data}));
                            }
                        )
                    }
                )
            }

    })
    



})